package com.neusoft.healthcity.config.scheduled;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.neusoft.healthcity.config.ReadJsonConfig;
import com.neusoft.healthcity.config.utils.BeanUtil;
import com.neusoft.healthcity.entity.InfectiousDiseasesReback;
import com.neusoft.healthcity.entity.jsonDeath.DeathRegList;
import com.neusoft.healthcity.entity.jsonInfectDIs.ContactTypeCodeList;

import com.neusoft.healthcity.service.InfectiousDiseasesRebackService;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Component
public class JsonCeneratorInfectious {

    @Autowired
    InfectiousDiseasesRebackService diseasesRebackService;

    @Value("${jsonRead.infectiousDiseases.jsonZipLocation}")
    private String zipPath;
    @Value("${jsonRead.infectiousDiseases.jsonFileDescLocation}")
    private String descDir;

    private Logger logger = LoggerFactory.getLogger(JsonCeneratorDeath.class);


    long startTime=0;
    long endTime=0;

    @Scheduled(cron = "0 */10 * * * ?")
    public void jsonGeneratorInfectiousScheduled() {
        logger.info("*************传染病返回数据获取定时任务开始*************");
        try {
            startTime = new Date().getTime();
            logger.info("*************"+startTime+"*************");
            logger.info("*************"+endTime+"*************");
            ReadJsonConfig.unZipFiles(zipPath, descDir,startTime,endTime);
            ArrayList<File> fileArrayList = new ArrayList<>();
            ArrayList<File> files = ReadJsonConfig.getAllFile(descDir, fileArrayList);
            for (int i = 0; i < files.size(); i++) {
                File file = files.get(i);

                String input = FileUtils.readFileToString(file, "UTF-8");
                JSONObject jsonObject = JSON.parseObject(input);


                JSONObject jsonArray2 = jsonObject.getJSONObject("InfectiousDiseases");
               InfectiousDiseasesReback diseasesReback = new InfectiousDiseasesReback();

                diseasesReback.setCreatedate(new Date());

                diseasesReback.setBusinessactivityidentification((jsonArray2.getString("BusinessActivityIdentification")));
                diseasesReback.setTotalrecords(jsonArray2.getString("TotalRecords"));
                diseasesReback.setStarttime(jsonArray2.getString("StartTime"));
                diseasesReback.setEndtime(jsonArray2.getString("EndTime"));

                JSONArray array = jsonObject.getJSONArray("InfectiousDiseasesList");
                for (int j = 0; j < array.size(); j++) {
                    JSONObject object = (JSONObject) array.get(j);
                    DeathRegList deathRegList = JSONObject.toJavaObject(object, DeathRegList.class);
                    BeanUtil.copyPropertiesIgnoreCase(deathRegList, diseasesReback);
                    JSONArray contactTypeCodeList = object.getJSONArray("ContactTypeCodeList");
                    for (int k = 0; k < contactTypeCodeList.size(); k++) {
                        JSONObject object1 = (JSONObject) contactTypeCodeList.get(k);
                        ContactTypeCodeList contactTypeCodeList1 = JSONObject.toJavaObject(object1, ContactTypeCodeList.class);
                        BeanUtil.copyPropertiesIgnoreCase(contactTypeCodeList1, diseasesReback);
                    }
                }

                //反射
                Class<? extends InfectiousDiseasesReback> iClass = diseasesReback.getClass();
                Field[] declaredFields = iClass.getDeclaredFields();
                for (int j = 0; j < declaredFields.length; j++) {
                    Field field = declaredFields[j];
                    field.setAccessible(true);
                    Object fieldVal = field.get(diseasesReback);
                    if (!"".equals(fieldVal) && null != fieldVal) {
                        String s = fieldVal.toString();
                        if (s.contains("{") || s.contains("}")) {
                            Object str = s.replace("{", "").replace("}", "");
                            field.set(diseasesReback, str);
                        }
                    }
                }

                //保存返回的数据
                diseasesRebackService.save(diseasesReback);

            }
            endTime = startTime;
            //删除解压目录
            File file = new File(descDir);
            ReadJsonConfig.deleteDir(file);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
